<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>导航网站</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            min-height: 100vh;
            padding: 20px;
            position: relative;
        }

        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.3);
            z-index: -1;
            display: none;
        }

        body.has-background::before {
            display: block;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
            color: white;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .search-box {
            margin-bottom: 30px;
            text-align: center;
        }

        .search-input {
            width: 100%;
            max-width: 500px;
            padding: 15px 20px;
            font-size: 16px;
            border: none;
            border-radius: 25px;
            outline: none;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }

        .search-input:focus {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(0,0,0,0.15);
        }

        .navigation-grid {
            display: grid;
            gap: 20px;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
        }

        .nav-item {
            background: white;
            border-radius: 12px;
            padding: 20px;
            text-decoration: none;
            color: #333;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            border-left: 4px solid #667eea;
        }

        .nav-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0,0,0,0.15);
            border-left-color: #764ba2;
        }

        .nav-item h3 {
            font-size: 1.2rem;
            margin-bottom: 8px;
            color: #2c3e50;
        }

        .nav-item p {
            color: #7f8c8d;
            font-size: 0.9rem;
            line-height: 1.4;
        }

        .admin-link {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background: #e74c3c;
            color: white;
            padding: 12px 20px;
            border-radius: 25px;
            text-decoration: none;
            font-weight: bold;
            box-shadow: 0 4px 15px rgba(231, 76, 60, 0.3);
            transition: all 0.3s ease;
        }

        .admin-link:hover {
            background: #c0392b;
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(231, 76, 60, 0.4);
        }

        .loading {
            text-align: center;
            color: white;
            font-size: 1.1rem;
            margin: 40px 0;
        }

        .error {
            text-align: center;
            color: #e74c3c;
            background: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .navigation-grid {
                grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
                gap: 15px;
            }
            
            .header h1 {
                font-size: 2rem;
            }
            
            .nav-item {
                padding: 15px;
            }
        }

        @media (max-width: 480px) {
            .navigation-grid {
                grid-template-columns: 1fr;
            }
            
            .header h1 {
                font-size: 1.8rem;
            }
            
            body {
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1 id="siteTitle">我的导航网站</h1>
            <p id="siteSubtitle">快速访问您收藏的网站</p>
        </div>

        <div class="search-box">
            <input type="text" class="search-input" placeholder="搜索网站..." id="searchInput">
        </div>

        <div id="navigationContainer">
            <div class="loading">正在加载导航数据...</div>
        </div>
    </div>

    <!-- 移除这行：<a href="/admin" class="admin-link">管理后台</a> -->

    <script>
        class NavigationApp {
            constructor() {
                this.navigationData = [];
                this.filteredData = [];
                this.searchInput = document.getElementById('searchInput');
                this.container = document.getElementById('navigationContainer');
                
                this.init();
            }

            async init() {
                await this.loadNavigationData();
                this.setupEventListeners();
                this.renderNavigation();
            }

            async loadNavigationData() {
                try {
                    const response = await fetch('/api/navigation');
                    if (!response.ok) {
                        throw new Error('网络请求失败');
                    }
                    const allData = await response.json();
                    // 只显示启用状态的网站
                    this.navigationData = allData.filter(item => item.status === 'enabled');
                    this.filteredData = [...this.navigationData];
                } catch (error) {
                    console.error('加载导航数据失败:', error);
                    this.showError('加载导航数据失败，请检查网络连接或稍后重试。');
                }
            }

            setupEventListeners() {
                this.searchInput.addEventListener('input', (e) => {
                    this.filterNavigation(e.target.value);
                });

                // 响应式布局调整
                window.addEventListener('resize', () => {
                    this.adjustLayout();
                });
            }

            filterNavigation(searchTerm) {
                const term = searchTerm.toLowerCase().trim();
                
                if (term === '') {
                    this.filteredData = [...this.navigationData];
                } else {
                    this.filteredData = this.navigationData.filter(item => 
                        item.title.toLowerCase().includes(term) ||
                        (item.description && item.description.toLowerCase().includes(term))
                    );
                }
                
                this.renderNavigation();
            }

            renderNavigation() {
                if (this.filteredData.length === 0) {
                    if (this.navigationData.length === 0) {
                        this.container.innerHTML = '<div class="error">暂无导航数据</div>';
                    } else {
                        this.container.innerHTML = '<div class="error">未找到匹配的网站</div>';
                    }
                    return;
                }

                const gridHtml = `
                    <div class="navigation-grid">
                        ${this.filteredData.map(item => this.createNavItem(item)).join('')}
                    </div>
                `;
                
                this.container.innerHTML = gridHtml;
                this.adjustLayout();
            }

            createNavItem(item) {
                const description = item.description || '点击访问该网站';
                return `
                    <a href="${item.url}" class="nav-item" target="_blank" rel="noopener noreferrer">
                        <h3>${this.escapeHtml(item.title)}</h3>
                        <p>${this.escapeHtml(description)}</p>
                    </a>
                `;
            }

            adjustLayout() {
                // 动态调整网格列数
                const grid = document.querySelector('.navigation-grid');
                if (!grid) return;

                const containerWidth = this.container.offsetWidth;
                const itemMinWidth = 280;
                const gap = 20;
                
                let columns = Math.floor((containerWidth + gap) / (itemMinWidth + gap));
                columns = Math.max(1, columns);
                
                grid.style.gridTemplateColumns = `repeat(${columns}, 1fr)`;
            }

            showError(message) {
                this.container.innerHTML = `<div class="error">${message}</div>`;
            }

            escapeHtml(text) {
                const div = document.createElement('div');
                div.textContent = text;
                return div.innerHTML;
            }
        }

        // 加载网站设置
        async function loadSiteSettings() {
            try {
                const response = await fetch('/api/settings');
                const settings = await response.json();
                
                // 设置网站标题和副标题
                if (settings.site_title) {
                    document.getElementById('siteTitle').textContent = settings.site_title;
                    document.title = settings.site_title;
                }
                if (settings.site_subtitle) {
                    document.getElementById('siteSubtitle').textContent = settings.site_subtitle;
                }
                
                // 设置背景图片
                if (settings.background_image) {
                    document.body.style.backgroundImage = `url('${settings.background_image}')`;
                    document.body.classList.add('has-background');
                }
            } catch (error) {
                console.error('加载网站设置失败:', error);
            }
        }

        // 初始化应用
        document.addEventListener('DOMContentLoaded', async () => {
            await loadSiteSettings();
            new NavigationApp();
        });
    </script>
</body>
</html>